<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>运维工作清单</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <link href="/css/bootstrap-icons.css" rel="stylesheet">
    <script src="/js/sweetalert2.js"></script>
    <script src="https://unpkg.com/echarts@5.4.3/dist/echarts.min.js"></script>
    <style>
        .summary-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            border: 1px solid rgba(255, 255, 255, 0.2);
            box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            overflow: hidden;
        }

        .summary-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 40px rgba(31, 38, 135, 0.25);
        }

        .section-title {
            position: relative;
            padding-left: 1rem;
            margin-bottom: 1.5rem;
            font-weight: 600;
            color: #1f2937;
        }

        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(to bottom, #6366f1, #4f46e5);
            border-radius: 2px;
        }

        .request-card {
            background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 1rem;
            border: none;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        .request-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        }

        .request-title {
            font-size: 1.1rem;
            font-weight: 600;
            color: #1f2937;
            margin-bottom: 1rem;
        }

        .request-info {
            display: flex;
            align-items: center;
            margin-bottom: 0.5rem;
            color: #6b7280;
        }

        .request-info i {
            width: 20px;
            margin-right: 0.5rem;
            color: #4f46e5;
        }

        .pagination-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 1rem;
            margin-top: 2rem;
        }

        .pagination {
            display: flex;
            align-items: center;
            background: #fff;
            border-radius: 8px;
            padding: 0.5rem;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            margin: 0;
        }

        .page-item {
            margin: 0 0.25rem;
        }

        .page-link {
            min-width: 36px;
            height: 36px;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0 0.75rem;
            border-radius: 6px;
            color: #4f46e5;
            font-weight: 500;
            transition: all 0.2s ease;
            border: none;
            background: transparent;
        }

        .page-link:hover:not(.disabled) {
            background: #f3f4f6;
            color: #4f46e5;
        }

        .page-item.active .page-link {
            background: #4f46e5;
            color: white;
        }

        .page-item.disabled .page-link {
            color: #9ca3af;
            cursor: not-allowed;
            pointer-events: none;
        }

        .page-info {
            color: #6b7280;
            font-size: 0.875rem;
            display: flex;
            justify-content: center;
            gap: 0.5rem;
        }

        .total-count {
            color: #9ca3af;
        }

        @media (max-width: 768px) {
            .pagination {
                padding: 0.25rem;
                flex-wrap: nowrap;
                justify-content: center;
            }

            .page-link {
                min-width: 32px;
                height: 32px;
                padding: 0 0.5rem;
                font-size: 0.875rem;
            }

            .page-info {
                font-size: 0.75rem;
            }

            /* 在移动端优化页码显示 */
            .page-item:not(.navigation):not(.active) {
                display: none;
            }

            .page-item.active,
            .page-item.navigation {
                display: flex;
            }

            /* 当前页码显示在中间 */
            .current-page-mobile {
                display: flex !important;
                align-items: center;
                padding: 0 0.5rem;
                color: #6b7280;
                font-size: 0.875rem;
            }
        }

        .chart-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
        }

        #monthlyChart {
            width: 100%;
            height: 250px;
        }
        #workloadChart {
            width: 100%;
            height: 250px;
        }
    </style>
</head>
<body>
<div class="page-background bg-requests"></div>
<div class="wave-bottom"></div>

<div class="slide-container" id="slideContainer">
    <div class="slide-content" id="slideContent">
        <div class="container mt-4">
            <!-- 月度分布图表 -->
            <div class="chart-card">
                <div class="section-title">
                    <i class="bi bi-bar-chart-line me-2"></i>年度运维工作月度分布情况
                </div>
                <div id="monthlyChart"></div>
            </div>

            <!-- 新增工时分布图表 -->
            <div class="chart-card">
                <div class="section-title">
                    <i class="bi bi-graph-up me-2"></i>年度运维工作月度工时分布情况
                </div>
                <div id="workloadChart"></div>
            </div>
            <div class="text-center py-4"  th:if="${topRequests.isEmpty()}">
                <i class="bi bi-clipboard-x text-muted" style="font-size: 2rem;"></i>
                <p class="text-muted mt-2">暂无运维工作清单</p>
            </div>
            <!-- 现有的需求列表部分 -->
            <div class="summary-card p-4" th:if="${!topRequests.isEmpty()}">
                <div class="section-title">
                    <i class="bi bi-clock-history me-2"></i>年度运维工作清单
                </div>

                <div class="requests-list">
                    <div id="requestsList">
                        <div th:each="request : ${topRequests}" class="request-card">
                            <div class="request-title" th:text="${request.demandName}">工作标题</div>
                            <div class="request-info">
                                <i class="bi bi-clock"></i>
                                <span>处理时长：</span>
                                <span th:text="${request.actualWorkload + '小时'}">0小时</span>
                            </div>
                            <div class="request-info">
                                <i class="bi bi-calendar-event"></i>
                                <span>完成时间：</span>
                                <span th:text="${#temporals.format(request.actualCompletionTime, 'yyyy-MM-dd HH:mm:ss')}">2024-01-01</span>
                            </div>
                            <div class="request-info">
                                <i class="bi bi-person"></i>
                                <span>处理人员：</span>
                                <span th:text="${request.taskCompleted}">处理人</span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 分页 -->
                <div class="pagination-container" th:if="${topRequests.totalPages > 1}">
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            <!-- 上一页 -->
                            <li class="page-item" th:classappend="${topRequests.first ? 'disabled' : ''}">
                                <a class="page-link" href="javascript:void(0);"
                                   th:onclick="'loadPage(' + ${topRequests.number - 1} + ')'"
                                   aria-label="Previous">
                                    <i class="bi bi-chevron-left"></i>
                                </a>
                            </li>

                            <!-- 首页 -->
                            <li class="page-item" th:classappend="${topRequests.number == 0 ? 'active' : ''}">
                                <a class="page-link" href="javascript:void(0);"
                                   th:onclick="'loadPage(0)'">1</a>
                            </li>

                            <!-- 省略号 -->
                            <li class="page-item disabled" th:if="${topRequests.number > 2}">
                                <span class="page-link">...</span>
                            </li>

                            <!-- 中间页码 -->
                            <li class="page-item"
                                th:each="pageNum : ${#numbers.sequence(
                                        T(java.lang.Math).max(1, topRequests.number - 1),
                                        T(java.lang.Math).min(topRequests.totalPages - 2, topRequests.number + 1))}"
                                th:classappend="${pageNum == topRequests.number ? 'active' : ''}">
                                <a class="page-link" href="javascript:void(0);"
                                   th:onclick="'loadPage(' + ${pageNum} + ')'"
                                   th:text="${pageNum + 1}">2</a>
                            </li>

                            <!-- 省略号 -->
                            <li class="page-item disabled"
                                th:if="${topRequests.number < topRequests.totalPages - 3}">
                                <span class="page-link">...</span>
                            </li>

                            <!-- 末页 -->
                            <li class="page-item"
                                th:if="${topRequests.totalPages > 1}"
                                th:classappend="${topRequests.number == topRequests.totalPages - 1 ? 'active' : ''}">
                                <a class="page-link" href="javascript:void(0);"
                                   th:onclick="'loadPage(' + ${topRequests.totalPages - 1} + ')'"
                                   th:text="${topRequests.totalPages}">3</a>
                            </li>

                            <!-- 下一页 -->
                            <li class="page-item" th:classappend="${topRequests.last ? 'disabled' : ''}">
                                <a class="page-link" href="javascript:void(0);"
                                   th:onclick="'loadPage(' + ${topRequests.number + 1} + ')'"
                                   aria-label="Next">
                                    <i class="bi bi-chevron-right"></i>
                                </a>
                            </li>
                        </ul>
                    </nav>

                    <!-- 页码信息 -->
<!--                    <div class="page-info">-->
<!--                        <span th:text="${topRequests.number + 1}">1</span> /-->
<!--                        <span th:text="${topRequests.totalPages}">1</span> 页-->
<!--                        <span class="total-count">共 <span th:text="${topRequests.totalElements}">0</span> 条</span>-->
<!--                    </div>-->
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 滑动提示 -->
<div class="slide-hint" id="slideHint"></div>

<!-- 页面指示器 -->
<div class="slide-indicator" id="slideIndicator">
    <div class="slide-dot"></div>
    <div class="slide-dot active"></div>
    <div class="slide-dot"></div>
    <div class="slide-dot"></div>
</div>

<script src="/js/jquery-3.6.0.min.js"></script>
<script src="/js/slide.js"></script>
<script>
    // 图表相关变量
    let monthlyChart = null;
    let workloadChart = null;
    let chartOption = null;
    let workloadOption = null;
    let isFirstLoad = true;

    // 从模板获取的数据
    let currentPageNum = "[[${topRequests.number}]]";
    const reportUuid = "[[${reportUuid}]]";
    const hospitalId = "[[${report.hospitalId}]]";
    const demandYear = "[[${report.demandYear}]]";

    // 加载分页数据
    function loadPage(pageNum) {
        // 从API获取数据
        fetch(`/api/tasks/${reportUuid}/${hospitalId}/${demandYear}/page?page=${pageNum}&size=3`)
            .then(response => response.json())
            .then(data => {
                currentPageNum = pageNum;
                updateRequestsList(data);
                updatePagination(data);
            })
            .catch(error => {
                console.error('Error loading page:', error);
            });
    }

    // 更新需求列表
    function updateRequestsList(data) {
        const requestsList = document.getElementById('requestsList');
        requestsList.innerHTML = data.content.map(request => `
            <div class="request-card">
                <div class="request-title">${request.demandName}</div>
                <div class="request-info">
                    <i class="bi bi-clock"></i>
                    <span>处理时长：</span>
                    <span>${request.actualWorkload}小时</span>
                </div>
                <div class="request-info">
                    <i class="bi bi-calendar-event"></i>
                    <span>完成时间：</span>
                    <span>${formatDateTime(request.actualCompletionTime)}</span>
                </div>
                <div class="request-info">
                    <i class="bi bi-person"></i>
                    <span>处理人员：</span>
                    <span>${request.taskCompleted}</span>
                </div>
            </div>
        `).join('');
    }

    // 更新分页组件
    function updatePagination(data) {
        const totalPages = data.totalPages;
        const currentPage = data.number;
        const totalElements = data.totalElements;

        let paginationHtml = `
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <!-- 上一页 -->
                    <li class="page-item navigation ${currentPage === 0 ? 'disabled' : ''}">
                        <a class="page-link" href="javascript:void(0);" 
                           onclick="loadPage(${currentPage - 1})" 
                           aria-label="Previous">
                            <i class="bi bi-chevron-left"></i>
                        </a>
                    </li>`;

        // PC端显示详细页码
        if (window.innerWidth > 768) {
            // 首页
            if (currentPage > 2) {
                paginationHtml += `
                    <li class="page-item">
                        <a class="page-link" href="javascript:void(0);" onclick="loadPage(0)">1</a>
                    </li>
                    <li class="page-item disabled">
                        <span class="page-link">...</span>
                    </li>`;
            }

            // 中间页码
            for (let i = Math.max(0, currentPage - 1); i <= Math.min(totalPages - 1, currentPage + 1); i++) {
                paginationHtml += `
                    <li class="page-item ${i === currentPage ? 'active' : ''}">
                        <a class="page-link" href="javascript:void(0);" onclick="loadPage(${i})">${i + 1}</a>
                    </li>`;
            }

            // 末页
            if (currentPage < totalPages - 3) {
                paginationHtml += `
                    <li class="page-item disabled">
                        <span class="page-link">...</span>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="javascript:void(0);" onclick="loadPage(${totalPages - 1})">${totalPages}</a>
                    </li>`;
            }
        }else {
            // 在移动端显示当前页/总页数 ${currentPage + 1}/${totalPages}
            paginationHtml += `
            <li class="page-item current-page-mobile">
                ${currentPage + 1}/${totalPages}
            </li>`;
        }

        paginationHtml += `
                <!-- 下一页 -->
                <li class="page-item navigation ${currentPage === totalPages - 1 ? 'disabled' : ''}">
                    <a class="page-link" href="javascript:void(0);" 
                       onclick="loadPage(${currentPage + 1})" 
                       aria-label="Next">
                        <i class="bi bi-chevron-right"></i>
                    </a>
                </li>
            </ul>
        </nav>
        <div class="page-info">
            <span>${currentPage + 1}</span> /
            <span>${totalPages}</span> 页
            <span class="total-count">共 <span>${totalElements}</span> 条</span>
        </div>`;

        document.querySelector('.pagination-container').innerHTML = paginationHtml;
    }

    // 格式化日期时间
    function formatDateTime(dateTimeStr) {
        if (!dateTimeStr) return '--';
        const date = new Date(dateTimeStr);
        return date.toLocaleString('zh-CN', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false
        }).replace(/\//g, '-');
    }

    // 初始化图表
    function initChart() {
        if (!isFirstLoad) return;
        
        monthlyChart = echarts.init(document.getElementById('monthlyChart'));

        // 显示加载中弹窗
        Swal.fire({
            title: '查询中...',
            allowOutsideClick: false,
            showConfirmButton: false,
            willOpen: () => {
                Swal.showLoading();
            }
        });

        // 加载数据并渲染图表
        fetch(`/api/tasks/${reportUuid}/${hospitalId}/${demandYear}/monthly-stats`)
            .then(response => response.json())
            .then(data => {
                // 关闭加载中弹窗
                Swal.close();

                const months = data.map(item => item.month + '月');
                const counts = data.map(item => item.count);

                chartOption = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        data: months,
                        axisLabel: {
                            interval: 0
                        }
                    },
                    yAxis: {
                        type: 'value',
                        name: '工作数量'
                    },
                    series: [{
                        name: '工作数量',
                        type: 'bar',
                        data: counts,
                        itemStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                {offset: 0, color: '#6366f1'},
                                {offset: 1, color: '#4f46e5'}
                            ])
                        },
                        emphasis: {
                            itemStyle: {
                                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    {offset: 0, color: '#7c7ff3'},
                                    {offset: 1, color: '#6c64e8'}
                                ])
                            }
                        },
                        label: { // 添加label配置项
                            show: true,
                            position: 'top',
                            formatter: '{c}'
                        }
                    }]
                };

                monthlyChart.setOption(chartOption);
                isFirstLoad = false;
            })
            .catch(error => {
                // 关闭加载中弹窗并显示错误信息
                Swal.fire({
                    icon: 'error',
                    title: '加载失败',
                    text: '查询月度运维数据发生错误，请稍后重试',
                    confirmButtonText: '确定'
                });
                console.error('Error loading monthly stats:', error);
            });
    }

    // 初始化工时图表
    function initWorkloadChart() {
        if (!isFirstLoad) return;
        
        workloadChart = echarts.init(document.getElementById('workloadChart'));

        // 加载数据并渲染图表
        fetch(`/api/tasks/${reportUuid}/${hospitalId}/${demandYear}/monthly-workload`)
            .then(response => response.json())
            .then(data => {
                const months = data.map(item => item.month + '月');
                const workloads = data.map(item => item.workload);

                workloadOption = {
                    tooltip: {
                        trigger: 'axis',
                        formatter: function(params) {
                            return params[0].name + '<br/>' +
                                params[0].seriesName + ': ' +
                                params[0].value.toFixed(2) + '小时';
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        data: months,
                        axisLabel: {
                            interval: 0
                        }
                    },
                    yAxis: {
                        type: 'value',
                        name: '工时（小时）'
                    },
                    series: [{
                        name: '工时',
                        type: 'line',
                        data: workloads,
                        smooth: true,
                        symbol: 'circle',
                        symbolSize: 8,
                        itemStyle: {
                            color: '#4f46e5'
                        },
                        lineStyle: {
                            width: 3,
                            color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                                {offset: 0, color: '#6366f1'},
                                {offset: 1, color: '#4f46e5'}
                            ])
                        },
                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                {offset: 0, color: 'rgba(99, 102, 241, 0.3)'},
                                {offset: 1, color: 'rgba(79, 70, 229, 0.1)'}
                            ])
                        },
                        label: {
                            show: true,
                            position: 'top',
                            formatter: function(params) {
                                return params.value.toFixed(2);
                            }
                        }
                    }]
                };

                workloadChart.setOption(workloadOption);
            })
            .catch(error => {
                console.error('Error loading monthly workload stats:', error);
            });
    }

    document.addEventListener('DOMContentLoaded', function() {
        initChart();
        initWorkloadChart();

        // 监听窗口大小变化
        window.addEventListener('resize', () => {
            if (monthlyChart) {
                monthlyChart.resize();
            }
            if (workloadChart) {
                workloadChart.resize();
            }
            // 重新渲染分页组件以适应屏幕大小
            loadPage(currentPageNum);
        });

        // 初始加载第一页数据
        loadPage(currentPageNum);
    });
</script>
</body>
</html> 